package com.haoniu.web.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.haoniu.common.utils.huanxin.HuanXinConstants;
import com.haoniu.common.utils.huanxin.HuanXinUtils;
import com.haoniu.module.domain.database.Room;
import com.haoniu.module.domain.database.RoomNotice;
import com.haoniu.module.domain.database.User;
import com.haoniu.module.service.RoomNoticeService;
import com.haoniu.module.service.RoomService;
import com.haoniu.module.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.haoniu.common.annotation.Log;
import com.haoniu.common.base.AjaxResult;
import com.haoniu.common.enums.BusinessType;
import com.haoniu.framework.util.ShiroUtils;
import com.haoniu.framework.web.page.TableDataInfo;
import com.haoniu.system.domain.SysNotice;
import com.haoniu.system.service.ISysNoticeService;
import com.haoniu.web.core.base.BaseController;

/**
 * 公告 信息操作处理
 * 
 * @author haoniu
 */
@Controller
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController
{
    private String prefix = "system/notice";

    @Autowired
    private ISysNoticeService noticeService;
    @Autowired
    private UserService userService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private RoomNoticeService roomNoticeService;

    @RequiresPermissions("system:notice:view")
    @GetMapping()
    public String notice()
    {
        return prefix + "/notice";
    }

    /**
     * 查询公告列表
     */
    @RequiresPermissions("system:notice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysNotice notice)
    {
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 新增公告
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存公告
     */
    @RequiresPermissions("system:notice:add")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysNotice notice)
    {
        notice.setCreateBy(ShiroUtils.getLoginName());

        Integer noticeNum = noticeService.insertNotice(notice);

        //判断是是否是公告信息
        if ("2".equals(notice.getNoticeType())){
            //查询公告userid
            List<User> noticeUsers = userService.findNoticeUser();

            //判断是否有公告id
            if (noticeUsers.size()==0){
                return toAjax(-1);
            }

            //公告用户id
            Integer noticeUserId = -1;
            User noticeUser = new User();
            for (int i=0;i<noticeUsers.size();i++){
                noticeUserId = noticeUsers.get(i).getId();
                noticeUser = noticeUsers.get(i);
            }

            if (noticeUserId<1){
                return toAjax(-1);
            }
            String noticeUserIdStr = noticeUserId.toString();

            //查询所有用户id
            List<Integer> userList = userService.findAllUser();
            if (userList.size()==0){
                return toAjax(-1);
            }

            String [] userIds = new String[userList.size()];

            for (int i=0;i<userList.size();i++){
                userIds[i] = userList.get(i).toString();
            }

            //公告客服推送公告信息给所有普通用户
            //发送环信消息
            Map<String, String> ext = new HashMap<>(16);
            ext.put("id", noticeUser.getId() + "");
            ext.put("nickname", noticeUser.getNickName());
            ext.put("headImg", noticeUser.getHeadImg());

            HuanXinUtils.sendUserTextMessage(noticeUserIdStr,notice.getNoticeContent(),userIds,ext);

        }else if ("3".equals(notice.getNoticeType())){
            //删除已有的公告信息
            List<RoomNotice> roomNoticeList = roomNoticeService.listRoomNotice();
            if (roomNoticeList.size()!=0){
                for (int i=0;i<roomNoticeList.size();i++){
                    roomNoticeService.deleteRoomNotice(roomNoticeList.get(i).getId());
                }
            }

            //查询最新房间公告信息
            List<SysNotice> sysNoticeList = noticeService.selectNotice("3");

            //判断最新房间公告信息是否存在
            Integer noticeId = 0;
            if (sysNoticeList.size()==0){
                return toAjax(-1);
            }
            //获取noticeId
            for (int i=0;i<sysNoticeList.size();i++){
                if (i==0){
                    noticeId = sysNoticeList.get(i).getNoticeId();
                }
            }

            List<Room> roomList = roomService.findAllRoom();
                //新增房间公告信息
            for (int i=0;i<roomList.size();i++){
                RoomNotice roomNotice = new RoomNotice();
                roomNotice.setNoticeId(noticeId);
                roomNotice.setRoomId(roomList.get(i).getId());
                roomNoticeService.insertRoomNotice(roomNotice);
            }
        }
        return toAjax(noticeNum);
    }

    /**
     * 修改公告
     */
    @GetMapping("/edit/{noticeId}")
    public String edit(@PathVariable("noticeId") Long noticeId, ModelMap mmap)
    {
        SysNotice sysNotice = noticeService.selectNoticeById(noticeId);
        mmap.put("notice",sysNotice );
        return prefix + "/edit";
    }

    /**
     * 修改保存公告
     */
    @RequiresPermissions("system:notice:edit")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysNotice notice)
    {
        notice.setUpdateBy(ShiroUtils.getLoginName());
        Integer noticeNum = noticeService.updateNotice(notice);

        //判断是是否是公告信息
        if ("2".equals(notice.getNoticeType())){
            //查询公告userid
            List<User> noticeUsers = userService.findNoticeUser();

            //判断是否有公告id
            if (noticeUsers.size()==0){
                return toAjax(-1);
            }

            //公告用户id
            Integer noticeUserId = -1;
            User noticeUser = new User();
            for (int i=0;i<noticeUsers.size();i++){
                noticeUser = noticeUsers.get(i);
                noticeUserId = noticeUsers.get(i).getId();
            }

            if (noticeUserId<1){
                return toAjax(-1);
            }
            String noticeUserIdStr = noticeUserId.toString();

            //查询所有用户id
            List<Integer> userList = userService.findAllUser();
            if (userList.size()==0){
                return toAjax(-1);
            }

            String [] userIds = new String[userList.size()];

            for (int i=0;i<userList.size();i++){
                userIds[i] = userList.get(i).toString();
            }

            //公告客服推送公告信息给所有普通用户
            //发送环信消息
            Map<String, String> ext = new HashMap<>(16);
            ext.put("id", noticeUser.getId() + "");
            ext.put("nickname", noticeUser.getNickName());
            ext.put("headImg", noticeUser.getHeadImg());


            HuanXinUtils.sendUserTextMessage(noticeUserIdStr,notice.getNoticeContent(),userIds,ext);

        }else if ("3".equals(notice.getNoticeType())){
            //删除已有的公告信息
            List<RoomNotice> roomNoticeList = roomNoticeService.listRoomNotice();
            if (roomNoticeList.size()!=0){
                for (int i=0;i<roomNoticeList.size();i++){
                    roomNoticeService.deleteRoomNotice(roomNoticeList.get(i).getId());
                }
            }

            //查询最新房间公告信息
            List<SysNotice> sysNoticeList = noticeService.selectNotice("3");

            //判断最新房间公告信息是否存在
            Integer noticeId = 0;
            if (sysNoticeList.size()==0){
                return toAjax(-1);
            }
            //获取noticeId
            for (int i=0;i<sysNoticeList.size();i++){
                if (i==0){
                    noticeId = sysNoticeList.get(i).getNoticeId();
                }
            }

            List<Room> roomList = roomService.findAllRoom();
            //更新房间公告信息
            for (int i=0;i<roomList.size();i++){
                RoomNotice roomNotice = new RoomNotice();
                roomNotice.setNoticeId(noticeId);
                roomNotice.setRoomId(roomList.get(i).getId());
                roomNoticeService.updateRoomNotice(roomNotice);
            }
        }
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 删除公告
     */
    @RequiresPermissions("system:notice:remove")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(noticeService.deleteNoticeByIds(ids));
    }
}
